﻿using System.Web;
using System.Configuration;
using System;

namespace Common
{
    public class CookieManage
    {
        /// <summary>
        /// Cookies赋值
        /// </summary>
        /// <param name="CookieName">主键</param>
        /// <param name="CookieValue">键值</param>
        /// <param name="Expires">有效天数</param>
        /// <returns></returns>
        public static void SetCookie(string CookieName, string CookieValue, double Expires)//, int strDay
        {
            HttpCookie Cookie = new HttpCookie(CookieName);
            Cookie.Domain = ConfigurationManager.AppSettings["CookieDomain"].ToString();//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            Cookie.Path = "/";
            if (Expires > 0)
            {
                Cookie.Expires = DateTime.Now.AddMinutes(Expires);
            }
            Cookie.Value = System.Web.HttpContext.Current.Server.UrlEncode(CookieValue);
            System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
        }

        /// <summary>
        /// 设置带子属性的Cookie
        /// </summary>
        /// <param name="CookieName">主要名字</param>
        /// <param name="ItemName">子属性名字</param>
        /// <param name="ItemValue">子属性值</param>
        public static void SetSubCookie(string CookieName, string ItemName, string ItemValue)
        {
            SetSubCookie(CookieName, ItemName, ItemValue, 600);
        }

        /// <summary>
        /// 设置带子属性的Cookie
        /// </summary>
        /// <param name="CookieName">主要名字</param>
        /// <param name="ItemName">子属性名字</param>
        /// <param name="ItemValue">子属性值</param>
        public static void SetSubCookie(string CookieName, string ItemName, string ItemValue, double Expires)
        {
            HttpCookie Cookie;
            if (System.Web.HttpContext.Current.Request.Cookies[CookieName] == null)
            {
                Cookie = new HttpCookie(CookieName);
                if (Expires > 0)
                {
                    Cookie.Expires = DateTime.Now.AddMinutes(Expires);
                }
                Cookie.Domain = ConfigurationManager.AppSettings["CookieDomain"].ToString();
            }
            else
            {
                Cookie = HttpContext.Current.Request.Cookies[CookieName];
            }

            Cookie.Values[ItemName] = System.Web.HttpContext.Current.Server.UrlEncode(ItemValue);
            System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
        }

        /// <summary>
        /// 读取单个Cookies
        /// </summary>
        /// <param name="CookieName">主键</param>
        /// <returns></returns>
        public static string GetCookie(string CookieName)
        {
            HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[CookieName];
            if (Cookie != null)
            {
                return System.Web.HttpContext.Current.Server.UrlDecode(Cookie.Value).ToString();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 读取制定Cookie下的属性
        /// </summary>
        /// <param name="CookieName"></param>
        /// <param name="ItemName"></param>
        /// <returns></returns>
        public static string GetSubCookie(string CookieName, string ItemName)
        {
            if (System.Web.HttpContext.Current.Request.Cookies[CookieName] != null)
            {
                if (System.Web.HttpContext.Current.Request.Cookies[CookieName].Values[ItemName] != null)
                {
                    return System.Web.HttpContext.Current.Server.UrlDecode(System.Web.HttpContext.Current.Request.Cookies[CookieName].Values[ItemName]).ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 删除Cookies
        /// </summary>
        /// <param name="strName">主键</param>
        /// <returns></returns>
        public static bool delCookie(string strName)
        {
            try
            {
                if (CheckCookie(strName))
                {
                    HttpCookie Cookie = new HttpCookie(strName);
                    Cookie.Domain = ConfigurationManager.AppSettings["CookieDomain"].ToString();
                    Cookie.Expires = DateTime.Now.AddDays(-1);
                    System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                    Cookie[strName] = null;
                    //HttpContext.Current.Request.Cookies[strName].Name = null;
                    HttpContext.Current.Request.Cookies.Remove(strName);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 判断是否有这个Cookie名称
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool CheckCookie(string name)
        {
            if (System.Web.HttpContext.Current.Request.Cookies[name] == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
